/**
 * Created by MacBook on 2019/3/29.
 */
    var imgSrc = []; //本地图片路径
    // 5 图片接口
    var images = {
        localId: [],
        serverId: []
    };


    /**
     * 图片上传
     * @param btnId 图片展示所在div位置的id
     * @param inputVal 点击按钮的value值  用来判读是否需要进行营业执照识别
     */
    function upload(btnId,inputVal) {
        wx.chooseImage({
            count: 1, // 默认9
            sizeType:'compressed',
            success: function (res) {
                images.localId = res.localIds;
                imgSrc = images.localId;
                //上传并将值设置在前端
                syncUpload(btnId,imgSrc,inputVal);
            }
        });
    };

    //图片上传到服务器
    function syncUpload(btnId,imgSrc,inputVal){
        var localId = imgSrc.pop();
        wx.uploadImage({
            localId: localId,
            isShowProgressTips: 1,
            sizeType:'compressed',
            success: function (res) {
                var serverId = res.serverId; // 返回图片的服务器端ID
                //图片展示
                addNewContent(btnId,localId,serverId);
                if("true"==inputVal){
                    //营业执照上传后自动识别
                    getPicMsg(serverId);
                }
            }
        });
    }



    //图片展示
    function addNewContent(btnId,localId,serverId) {
        var ua = navigator.userAgent.toLowerCase();
        var ios;
        console.log(ua);
        if (/iphone|ipad|ipod/.test(ua)) {
            ios = true;
        } else {
            ios = false;
        }
        //ios
        if (ios || window.__wxjs_is_wkwebview) {
            wx.getLocalImgData({
                localId: localId,
                success: function (res) {
                    var localData = res.localData; // localData是图片的base64数据，可以用img标签显示var str = '<div class="shangchuan_img_xx">'+
                    var imgbox_list = '<div class="imgContainer"><img src=' + localData + ' onclick="imgDisplay(this)"><input name='+btnId+' type="hidden" value='+serverId+'></div>';
                    $("#"+btnId).html(imgbox_list);
                },
                fail: function (res) {
                    alert('该图片暂时无法查看')
                }
            });
        }else{
            var imgbox_list = '<div class="imgContainer"><img src=' + localId + ' onclick="imgDisplay(this)"><input name='+btnId+' type="hidden" value='+serverId+'></div>';
            $("#"+btnId).html(imgbox_list);
        }
    }


//图片到后台解析，后去返回的营业执照信息
function getPicMsg(serverId) {
    if (serverId == "" || serverId == null) {
        alert("请上传图片");
        return false;
    }
    $.ajax({
        url: "getBLiscenseMsg",
        type: "POST",
        data: "pic=" + serverId,
        dataType: "json",
        success: function (result) {
           /* //先将里面的内容清空
            $('#enterpriseName').val();
            $('#legalPerson').val();
            $('#contactPerson').val();
            $('#enterpriseNum').val();
            $('#addr').val();*/

            var list = result.words_result;
            console.log(list);
            //营业执照信息插入输入框
            var enterpriseName = list["单位名称"].words;
            var legalPerson = list["法人"].words;
            var enterpriseNum = list["社会信用代码"].words;
            var addr = list["地址"].words;

            //将识别后的内容重新赋值给对应的id
            $('#enterpriseName').val(enterpriseName);
            $('#legalPerson').val(legalPerson);
            $('#contactPerson').val(legalPerson);
            $('#enterpriseNum').val(enterpriseNum);
            $('#addr').val(addr);

            $('#signboard').val(enterpriseName);
            $('#mdxfContactPerson').val(legalPerson);
            $('#mdxfEnterpriseNum').val(enterpriseNum);
            $('#mdxfAddr').val(addr);
        }
    });
}


